package com.djk;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class WordDao {
    private WordsDBHelper dbHelper;
    private static final String DATABASE_NAME = "WordsDB";

    public WordDao(Context context) {
        dbHelper = new WordsDBHelper(context, DATABASE_NAME, null, 1);
    }

    // 添加单词到数据库
    public long addWord(Word word) {

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(WordsDBHelper.COLUMN_WORD, word.getWord());
        values.put(WordsDBHelper.COLUMN_MEANING, word.getMeaning());

        return db.insert(WordsDBHelper.TABLE_NAME, null, values);
    }

    // 获取所有单词
    public List<Word> getAllWords() {
        List<Word> wordList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = null;

        try {
            cursor = db.query(
                    WordsDBHelper.TABLE_NAME,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null
            );
            if (cursor != null && cursor.moveToFirst()) {
                int idIndex = cursor.getColumnIndex(WordsDBHelper.COLUMN_ID); // 使用列名常量获取索引
                int wordIndex = cursor.getColumnIndex(WordsDBHelper.COLUMN_WORD);
                int meaningIndex = cursor.getColumnIndex(WordsDBHelper.COLUMN_MEANING);

                do {
                    int id = cursor.getInt(idIndex);
                    String word = cursor.getString(wordIndex);
                    String meaning = cursor.getString(meaningIndex);
                    wordList.add(new Word(id, word, meaning));
                } while (cursor.moveToNext());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return wordList;
    }

    //模糊查询
    public List<Word> searchWords(String keyword) {
        List<Word> wordList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = null;

        try {
            String selection = WordsDBHelper.COLUMN_WORD + " LIKE ?";
            String[] selectionArgs = new String[]{"%" + keyword + "%"};

            cursor = db.query(
                    WordsDBHelper.TABLE_NAME,
                    null,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    null
            );

            if (cursor != null && cursor.moveToFirst()) {
                int idIndex = cursor.getColumnIndex(WordsDBHelper.COLUMN_ID);
                int wordIndex = cursor.getColumnIndex(WordsDBHelper.COLUMN_WORD);
                int meaningIndex = cursor.getColumnIndex(WordsDBHelper.COLUMN_MEANING);

                do {
                    int id = cursor.getInt(idIndex);
                    String word = cursor.getString(wordIndex);
                    String meaning = cursor.getString(meaningIndex);
                    wordList.add(new Word(id, word, meaning));
                } while (cursor.moveToNext());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return wordList;
    }

    //按照id删除单词
    public boolean deleteById(int id) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String whereClause = WordsDBHelper.COLUMN_ID + "=?";
        String[] whereArgs = new String[]{String.valueOf(id)};

        int deletedRows = db.delete(WordsDBHelper.TABLE_NAME, whereClause, whereArgs);
        return deletedRows > 0;
    }

    //按照id更新单词信息
    public boolean updateWord(Word word) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(WordsDBHelper.COLUMN_WORD, word.getWord());
        values.put(WordsDBHelper.COLUMN_MEANING, word.getMeaning());

        String whereClause = WordsDBHelper.COLUMN_ID + "=?";
        String[] whereArgs = new String[]{String.valueOf(word.getId())};

        int updatedRows = db.update(WordsDBHelper.TABLE_NAME, values, whereClause, whereArgs);
        return updatedRows > 0;
    }

    // 检查单词是否存在的方法
    public boolean checkWordExists(String word) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = null;

        try {
            String selection = WordsDBHelper.COLUMN_WORD + "=?";
            String[] selectionArgs = new String[]{word};

            cursor = db.query(
                    WordsDBHelper.TABLE_NAME,
                    null,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    null
            );

            // 如果查询到结果，说明单词已存在
            if (cursor != null && cursor.getCount() > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return false;
    }

}

